Page({

  /**
   * 页面的初始数据
   */
  data: {
    incomeList: [],        //收入列表
    start: 0,              //从第几页开始
    length: 6,             //每页几条数据
    totalMoney: 0.00,      //收入
    userId:'',
    apiHost:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userId = wx.getStorageSync('userId');
    var apiHost = wx.getStorageSync('apiHost');
    // var userId = '2274c839ea984aacb4f1806323a96a0d';
    this.setData({
      userId: userId,
      apiHost: apiHost
    })

    that.incomeList(); //收入列表


  },
  //收入列表
  incomeList: function (userId) {
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    var that = this;
    var start = that.data.start;
    var length = that.data.length;
    var userId = that.data.userId;
    var apiHost = that.data.apiHost;
    wx.request({
      url: apiHost + 'hpp/user/balance/sellIncome',
      data: {
        userId: userId,
        start: start,
        length: length
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (data) {
        // console.log(data.data);
        var code = data.data.code;
        var msg = data.data.msg;

        if (code == "10000") {
          var totalMoney = data.data.data.totalMoney;
          var data = data.data.data.incomeList;

          that.setData({
            incomeList: data,
            totalMoney: totalMoney
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask:true
          })
        }
        // 隐藏加载框  
        wx.hideLoading();
      },
      fail: function () {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  //下拉加载更多
  onReachBottom: function () {
    var that = this;
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
      icon: 'loading',
      mask: true
    })

    var start = that.data.start;
    var length = that.data.length;
    var userId = that.data.userId;
    var apiHost = that.data.apiHost;
    // 页数+1 
    var start=start + 1
    that.setData({
      start: start + 1
    });


    wx.request({
      url: apiHost + 'hpp/user/balance/sellIncome',
      data: {
        start: start,
        length: length,
        userId: userId
      }, 
      header: {
        'content-type': 'application/json'
      },
      success: function (data) {
        // console.log(data.data.data.incomeList)
        var code = data.data.code;
        var msg = data.data.msg;
        

        if (code == '10000') {
          var data = data.data.data.incomeList;
          // 回调函数
          if (data != '') {
            var moment_list = that.data.incomeList;
            for (var i = 0; i < data.length; i++) {
              moment_list.push(data[i]);
            }
            // 设置数据  
            that.setData({
              incomeList: moment_list
            })
            // 隐藏加载框  
            wx.hideLoading();
          } else {
            // 隐藏加载框  
            wx.hideLoading();
            wx.showToast({
              title: '没有更多数据',
              icon: 'none',
              duration: 2000,
              mask: true
            })
          }
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
  }
})